package com.lt.shiropro.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lt.shiropro.entity.Category;
import com.lt.shiropro.entity.dto.CategoryDto;
import com.lt.shiropro.result.Result;
import com.lt.shiropro.service.CategoryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author：lt
 * @Package：com.lt.shiropro.controller
 * @Project：shiro-pro
 * @name：CategoryController
 * @Date：2023/12/18 10:50
 * @Filename：CategoryController
 */
@RestController
@RequestMapping("/category")
//@Api(tags="分类模块")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

//    @ApiOperation(value="新增分类", notes="新增分类")
    @PostMapping("/add")
    public Result<String> add(@RequestBody Category category){
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        category.setCategoryStatus(1);
        boolean save = categoryService.save(category);
        if(save){
            return Result.oK();
        }else {
            return Result.error("新增失败");
        }
    }
//    @ApiOperation(value="更新分类", notes="更新分类")
    @PostMapping("/update")
    public Result<String> update(@RequestBody Category category){
        category.setUpdateTime(new Date());
        boolean save = categoryService.saveOrUpdate(category);
        if(save){
            return Result.oK();
        }else {
            return Result.error("更新失败");
        }
    }

//    @ApiOperation(value="查询分类", notes="查询分类")
    @PostMapping("/list")
    public Result<List<Category>> list(@RequestBody CategoryDto categoryDto){
        String categoryName = categoryDto.getCategoryName();
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if(categoryName != null){
            wrapper.like(Category::getCategoryName,categoryName);
        }
        List<Category> list = categoryService.list(wrapper);
        return Result.OK(list);
    }
}
